export default{
    data(){
        let goods = {
            configHash:'',
            list:[],
            total:0,
            pageCount:0,
            page:1,
            errorMsg:[]
        }
        return {
            goods
        }
    },
    methods:{
        __configHash(config){
            if (config==undefined ) config = {};
            let  simpleHash = (inputString) => {
                
                let hash = 0;
                for (let i = 0; i < inputString.length; i++) {
                    const charCode = inputString.charCodeAt(i);
                    hash = ((hash << 5) - hash) + charCode; 
                    hash &= hash; 
                }
                return hash;
            }

           return simpleHash(JSON.stringify(config));
        },
        setGoodsParam(config){
            
            let hash  = this.__configHash(config);
            if (this.goods.configHash !=hash){
                this.goods.configHash = hash;
                this.getGoodsList(config,hash);
            }else{
                return ;
            }
        },
        getGoodsList(config,refresh=false){
            let key  =  'shop_preview_goods_list_'+this.__configHash(config);
            let setData = (data)=>{
                this.goods.list = data.list;
                this.goods.total = data.total;
                this.goods.pageCount = data.pageCount;
                this.goods.page = data.page;
            }

            if (this.$user.getLocalStorage(key) && !refresh ){
                setData(this.$user.getLocalStorage(key));
                return ;
            }

            this.list = [];
            this.$parent.modelLoading(this.modelIndex,true);
            this.$ajax({
                url:this.$url({app:'shop',controller:'api.goods',action:'list'}),
                data:{
                    ...config,
                    page:this.goods.page
                },
                success:(res)=>{
                    setData(res);
                    this.$user.setLocalStorage(key,res,3600);
                },
                over:()=>{
                    this.$parent.modelLoading(this.modelIndex,false);
                }
            });
        },
    },
    beforeMount(){
        this.$parent.modelCtrl(this.modelIndex,'refresh','fa-refresh',()=>{
            this.getGoodsList(this.config.goods,true);
        });
    },
    mounted(){
        
    }
}